from django.shortcuts import render, redirect

# Create your views here.

from django.http import JsonResponse

from web.views_dir import pub

from web import models

import os
import base64
import time


@pub.is_login
def test(request):

    if "_pjax" in request.GET:
        return render(request, 'test_pjax.html')

    return render(request, 'test.html')


@pub.is_login
def index(request):
    user_id = request.session['user_id']
    user_profile_obj = models.UserProfile.objects.get(id=user_id)
    if user_profile_obj.status == 1:
        return redirect('/account/password/')

    if "_pjax" in request.GET:
        return render(request, 'index_pjax.html')

    return render(request, 'index.html')


# 登录
def login(request):

    response = pub.BaseResponse()

    if request.method == "POST":
        name = request.POST.get('name')
        password = request.POST.get('password')

        print(pub.str_encrypt(password))

        user_profile_obj = models.UserProfile.objects.filter(
            name=name,
            password=pub.str_encrypt(password)
        )

        if user_profile_obj:

            if user_profile_obj[0].status == 3:
                response.status = False
                response.error['password'] = "用户已经锁定,请联系管理员解锁"
            else:
                request.session['name'] = name
                request.session['user_id'] = user_profile_obj[0].id
                request.session['is_login'] = True
                request.session['is_admin'] = user_profile_obj[0].type
                request.session['name'] = user_profile_obj[0].name
                response.status = True
        else:
            response.status = False
            response.error['password'] = "用户名密码不对"

        return JsonResponse(response.__dict__)

    else:
        if "name" in request.GET and "token" in request.GET:
            name = request.GET.get('name')
            token = request.GET.get('token')
            if token == "9044e28865c418da6951d9764062bf6b":
                user_profile_obj = models.UserProfile.objects.filter(name=name)
                print(user_profile_obj)
                if user_profile_obj:
                    if user_profile_obj[0].status == 3:
                        response.status = False
                        response.error['password'] = "用户已经锁定,请联系管理员解锁"
                    else:
                        request.session['name'] = name
                        request.session['user_id'] = user_profile_obj[0].id
                        request.session['is_login'] = True
                        request.session['is_admin'] = user_profile_obj[0].type
                        request.session['name'] = user_profile_obj[0].name
                        response.status = True

                        return redirect('/')

    return render(request, 'login.html')


# 登出
def logout(request):
    request.session.clear()

    return redirect('/account/login/')


# 图片上传
@pub.is_login
def img_upLoad(request):
    print(request.POST)
    response = pub.BaseResponse()
    imgBase = request.POST.get('imgBase')
    imgFormat = request.POST.get('imgFormat')
    lookIndex = request.POST.get('lookIndex')

    imgdata = base64.b64decode(imgBase.split(',')[1])
    img_name = str(int(time.time() * 1000000)) + '.' + imgFormat
    img_abs_name = os.path.join("statics/upload_images", img_name)
    with open(img_abs_name, "wb") as f:
        f.write(imgdata)

    response.status = True
    response.ind = lookIndex
    response.img_url = '/' + img_abs_name
    return JsonResponse(response.__dict__)


# 密码管理
@pub.is_login
def password(request):
    response = pub.BaseResponse()

    if request.method == "POST":
        new_password = request.POST.get('new_password')

        response.status = True
        response.message = "修改成功"

        if new_password:
            user_id = request.session['user_id']
            user_profile_obj = models.UserProfile.objects.get(id=user_id)
            password = pub.str_encrypt(request.POST.get('new_password'))
            user_profile_obj.password = password
            user_profile_obj.status = 2
            user_profile_obj.save()
        else:
            response.status = False
            response.message = "密码不能为空"

        return JsonResponse(response.__dict__)

    if "_pjax" in request.GET:
        return render(request, 'password_pjax.html')

    return render(request, 'password.html')
